package com.fone.player.airone;

import android.content.Context;
import android.media.AudioManager;
import android.os.Handler;
import android.os.Looper;

import com.fone.player.play.FonePlayerActivity;
import com.fone.player.util.L;

public class FDLNARender {

	static FDLNARender instance;
	private FDLNARender(){
		//CDLNAMediaRender mRender = new CDLNAMediaRender(this,FonePlayerActivity.instance);
		//mRender.start();
	}
	public static FDLNARender getInstance(){
		if( instance == null )
			instance = new FDLNARender();
		return instance;
	}
	
	public static enum dlna{
		dlna_render_getPlayState,
		dlna_render_getPlayVolume,
		dlna_render_getplaycurtime,
		dlna_render_getvideoduration,
		dlna_render_init,
		dlna_render_play,
		dlna_render_playorpause,
		dlna_render_seek,
		dlna_render_setPlayVolume,
		dlna_render_stop,
		dlna_render_setTitle,
		dlna_render_setIcon,
		dlna_on_open_success,
		dlna_on_open_error,
	}
	
	private int call(dlna type,String value){
		return 0;
		//TODO 实现调用各种状态
//		return fn_Core.dlna_render_event(type.ordinal(),value);
	}
	 
	public int getPlayState() {
		return call(dlna.dlna_render_getPlayState, null);
	}
	 
	 
	public String getplaycurtime() {
		
		return getFormatTime(call(dlna.dlna_render_getplaycurtime, null)/1000);
	}

	 
	public String getvideoduration() {
		int duration = call(dlna.dlna_render_getvideoduration, null);
		String str = getFormatTime(duration/1000);
		L.v("airone", "dlna_render_getvideoduration second:",duration);
		L.v("airone", "dlna_render_getvideoduration str:", str);
		return str;
	}

	 
	public void init() {
		call(dlna.dlna_render_init, null);
	}

//	 
//	public boolean play(String arg0,String title) {
//		return call(dlna.dlna_render_play, arg0)==1?true:false;
//	}

	 
	public boolean playorpause(boolean arg0) {
		return call(dlna.dlna_render_playorpause, arg0?"1":"0")==1?true:false;
	}

	 
	public boolean seek(String arg0) {
		String[] time = arg0.split(":");
		int second = 0;
		int unit = 1;
		for(int i=time.length-1;i>-1;i--){
			second += Integer.parseInt(time[i])*unit;
			unit *= 60;
		}
		return call(dlna.dlna_render_seek, String.valueOf(second*1000))==1?true:false;
	}

	 
	public boolean stop() {
		
		new Handler(Looper.getMainLooper()).post(new Runnable(){
			public void run(){
				call(dlna.dlna_render_stop,null);
			}
		});
		return 1==1?true:false;
		
		//return  call(dlna.dlna_render_stop,null)==1?true:false;
	}

	private String getFormatTime(int second)
	{
		int min = second / 60;
		int hour = min / 60;
		min = min % 60;
		int ss  = second % 60;
		return String.format("%1$02d:%2$02d:%3$02d", hour, min, ss);
	}
	 
	public boolean play(final String arg0, String arg1, String arg2, long arg3) {
		call(dlna.dlna_render_setTitle, arg1);
		call(dlna.dlna_render_setIcon, arg2);
		
		new Handler(Looper.getMainLooper()).post(new Runnable(){
			public void run(){
				call(dlna.dlna_render_play, arg0);
			}
		});
		return 1==1?true:false;
		
	}
	public void dlna_on_open_success(String uuid){
		call(dlna.dlna_on_open_success,uuid);
	}
	public void dlna_on_open_error(String uuid){
		call(dlna.dlna_on_open_error,uuid);
	}
	 
	public String getCurrentURI() {
		return null;
	}
	 
	public boolean playaudio(String arg0, String arg1, String arg2, long arg3) {
		return false;
	}
	 
	public boolean playpic(String arg0, String arg1) {
		return false;
	}

	
	
}
